using System;
using System.Collections.Generic;
using Volo.Abp.Application.Dtos;
using TenantManagementService.Tenants;

namespace TenantManagementService.Monitoring
{
    /// <summary>
    /// 租户使用统计
    /// </summary>
    public class TenantUsageStatistics
    {
        public Guid TenantId { get; set; }
        public string TenantName { get; set; } = string.Empty;
        public int UserCount { get; set; }
        public int MaxUserCount { get; set; }
        public double UserUsagePercentage { get; set; }
        public long StorageUsage { get; set; }
        public long MaxStorageSize { get; set; }
        public double StorageUsagePercentage { get; set; }
        public long ApiCallCount { get; set; }
        public double AverageApiResponseTime { get; set; }
        public double ApiErrorRate { get; set; }
        public DateTime LastAccessTime { get; set; }
        public TenantStatus Status { get; set; }
        public DateTime? SubscriptionEndTime { get; set; }
        public int DaysUntilExpiration { get; set; }
    }

    /// <summary>
    /// 租户概览统计
    /// </summary>
    public class TenantsOverviewStatistics
    {
        public int TotalTenants { get; set; }
        public int ActiveTenants { get; set; }
        public int TrialTenants { get; set; }
        public int SuspendedTenants { get; set; }
        public int ExpiredTenants { get; set; }
        public int ExpireSoonTenants { get; set; } // 7天内过期
        public int OverUserLimitTenants { get; set; }
        public int OverStorageLimitTenants { get; set; }
        public long TotalApiCalls { get; set; }
        public double AverageApiResponseTime { get; set; }
        public double OverallApiErrorRate { get; set; }
        public DateTime StatisticsTime { get; set; }
    }

    /// <summary>
    /// 租户资源使用情况
    /// </summary>
    public class TenantResourceUsage
    {
        public Guid TenantId { get; set; }
        public string TenantName { get; set; } = string.Empty;
        public TenantStatus Status { get; set; }
        public ResourceUsage UserUsage { get; set; } = new();
        public ResourceUsage StorageUsage { get; set; } = new();
        public ResourceUsage ApiUsage { get; set; } = new();
        public DateTime LastUpdated { get; set; }
    }

    /// <summary>
    /// 资源使用情况
    /// </summary>
    public class ResourceUsage
    {
        public long Current { get; set; }
        public long Max { get; set; }
        public double Percentage { get; set; }
        public bool IsExceeded { get; set; }
        public bool IsWarning { get; set; } // 超过80%
        public bool IsCritical { get; set; } // 超过95%
    }

    /// <summary>
    /// 创建告警规则输入
    /// </summary>
    public class CreateAlertRuleInput
    {
        public string Name { get; set; } = string.Empty;
        public string DisplayName { get; set; } = string.Empty;
        public string? Description { get; set; }
        public string MetricName { get; set; } = string.Empty;
        public AlertLevel Level { get; set; }
        public string Operator { get; set; } = "gt";
        public decimal Threshold { get; set; }
        public int Duration { get; set; } = 60;
        public int EvaluationInterval { get; set; } = 30;
        public Dictionary<string, string>? LabelFilters { get; set; }
        public List<string>? NotificationChannels { get; set; }
        public string? Expression { get; set; }
    }

    /// <summary>
    /// 更新告警规则输入
    /// </summary>
    public class UpdateAlertRuleInput
    {
        public string DisplayName { get; set; } = string.Empty;
        public string? Description { get; set; }
        public string Operator { get; set; } = "gt";
        public decimal Threshold { get; set; }
        public int Duration { get; set; } = 60;
        public int EvaluationInterval { get; set; } = 30;
        public Dictionary<string, string>? LabelFilters { get; set; }
        public List<string>? NotificationChannels { get; set; }
        public string? Expression { get; set; }
        public bool IsEnabled { get; set; } = true;
    }

    /// <summary>
    /// 获取告警规则输入
    /// </summary>
    public class GetAlertRulesInput : PagedAndSortedResultRequestDto
    {
        public string? Filter { get; set; }
        public AlertLevel? Level { get; set; }
        public bool? IsEnabled { get; set; }
        public string? MetricName { get; set; }
    }

    /// <summary>
    /// 获取活跃告警输入
    /// </summary>
    public class GetActiveAlertsInput : PagedAndSortedResultRequestDto
    {
        public string? Filter { get; set; }
        public AlertLevel? Level { get; set; }
        public Guid? TenantId { get; set; }
        public AlertStatus? Status { get; set; }
    }

    /// <summary>
    /// 处理告警输入
    /// </summary>
    public class HandleAlertInput
    {
        public string Assignee { get; set; } = string.Empty;
        public string Note { get; set; } = string.Empty;
    }

    /// <summary>
    /// 告警统计
    /// </summary>
    public class AlertStatistics
    {
        public int TotalAlerts { get; set; }
        public int ActiveAlerts { get; set; }
        public int ResolvedAlerts { get; set; }
        public int IgnoredAlerts { get; set; }
        public int CriticalAlerts { get; set; }
        public int WarningAlerts { get; set; }
        public int InfoAlerts { get; set; }
        public int EmergencyAlerts { get; set; }
        public double AverageResolutionTime { get; set; } // 分钟
        public Dictionary<AlertLevel, int> AlertsByLevel { get; set; } = new();
        public Dictionary<string, int> AlertsByMetric { get; set; } = new();
    }

    /// <summary>
    /// 系统健康状态
    /// </summary>
    public class SystemHealthStatus
    {
        public HealthStatus OverallStatus { get; set; }
        public Dictionary<string, HealthStatus> ComponentStatus { get; set; } = new();
        public Dictionary<string, object> Metrics { get; set; } = new();
        public DateTime LastCheckTime { get; set; }
        public List<string> Issues { get; set; } = new();
    }

    /// <summary>
    /// 指标数据点
    /// </summary>
    public class MetricDataPoint
    {
        public DateTime Timestamp { get; set; }
        public decimal Value { get; set; }
        public Dictionary<string, string>? Dimensions { get; set; }
    }

    /// <summary>
    /// 指标记录输入
    /// </summary>
    public class MetricRecordInput
    {
        public string MetricName { get; set; } = string.Empty;
        public decimal Value { get; set; }
        public DateTime Timestamp { get; set; } = DateTime.Now;
        public Dictionary<string, string>? Dimensions { get; set; }
        public Guid? TenantId { get; set; }
    }
}